package com.joshua.spring.entity.em;

import com.joshua.spring.exception.ExceptionEnumInter;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.http.HttpStatus;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * description
 * package com.joshua.spring.entity.enums
 * classname  ResponseEnum
 *
 * @author <a href="mailto:joshualwork@163.com">joshua_liu</a>
 * @date 2020/3/30 16:19
 */
@Getter
@AllArgsConstructor
public enum ResponseEnum implements ExceptionEnumInter {
    //成功
    SUCCESS("00000", "成功", HttpStatus.OK),

    //用户
    NULL_USERNAME_PWD("A0110", "用户名或密码校验失败", HttpStatus.UNAUTHORIZED),
    ERROR_VERIFY_CODE("A0130 ", "无效的校验码", HttpStatus.BAD_REQUEST),
    NULL_PHONE_NUM("A0151", "手机号格式校验失败", HttpStatus.BAD_REQUEST),
    NULL_EMAIL("A0153", "邮箱格式校验失败", HttpStatus.BAD_REQUEST),
    USER_NOT_FOUND("A0201", "用户账户不存在", HttpStatus.UNAUTHORIZED),
    AUTHORITY_EXCEPTION("A0300 ", "访问权限异常", HttpStatus.UNAUTHORIZED),
    AUTHORITY_FORBIDDEN("A0301 ", "访问未授权", HttpStatus.UNAUTHORIZED),
    TOKEN_EXPIRE("A0311 ", "授权已过期", HttpStatus.UNAUTHORIZED),
    ILLEGAL_REQUEST_PARAM("A0400", "请求参数错误", HttpStatus.BAD_REQUEST),
    UNAUTHORIZED("A0401", "未认证", HttpStatus.UNAUTHORIZED),
    FORBIDDEN("A0403", "非法请求", HttpStatus.FORBIDDEN),
    METHOD_NOT_ALLOW("A0405", "错误的Http请求方式", HttpStatus.METHOD_NOT_ALLOWED),
    USER_REQUEST_SERVER_FAIL("A0500", "用户请求服务异常", HttpStatus.INTERNAL_SERVER_ERROR),

    NOT_FOUND("B0404", "系统资源不存在", HttpStatus.NOT_FOUND),
    JOB_FAIL("B0330", "job异常", HttpStatus.INTERNAL_SERVER_ERROR),
    FAIL("B0500", "系统执行出错", HttpStatus.INTERNAL_SERVER_ERROR),
    //The service or item was not found
    SERVICE_UNAVAILABLE("C0404", "服务实例不可用", HttpStatus.SERVICE_UNAVAILABLE),
    REQUEST_BREAKING("C0104", "请求被熔断", HttpStatus.GATEWAY_TIMEOUT),
    GATEWAY_TIMEOUT("C0504", "网关请求超时", HttpStatus.GATEWAY_TIMEOUT),
    ;
    private String code;
    private String msgFormatter;
    private HttpStatus httpStatus;

    public static List<String> getRequestBreakingCodes() {
        List<String> result = new ArrayList<>();
        result.add(REQUEST_BREAKING.toString());
        result.add(USER_REQUEST_SERVER_FAIL.toString());
        result.add(FAIL.toString());
        return result;
    }

    public static List<ResponseEnum> getResultEnumsByHttpStatus(HttpStatus httpStatus) {
        List<ResponseEnum> list = new ArrayList<>();
        ResponseEnum[] values = ResponseEnum.values();
        for (int i = 0; i < values.length; i++) {
            if (values[i].httpStatus.equals(httpStatus)) {
                list.add(values[i]);
            }
        }
        return list;
    }

    public static ResponseEnum getResultEnumByHttpStatus(HttpStatus httpStatus) {
        List<ResponseEnum> list = getResultEnumsByHttpStatus(httpStatus);
        if (CollectionUtils.isEmpty(list)) {
            return ResponseEnum.FAIL;
        }
        return list.get(list.size() - 1);
    }

    public static ResponseEnum getResultEnumByCode(String code) {
        ResponseEnum[] values = ResponseEnum.values();
        for (int i = 0; i < values.length; i++) {
            if (values[i].code.equals(code)) {
                return values[i];
            }
        }
        return ResponseEnum.FAIL;
    }

}
